package com.so206.controller.payment;

import com.so206.po.SmsConfig;
import com.so206.service.SMSConfig;
import com.so206.utils.MailUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 短信邮件配置
 */
@Controller
@RequestMapping("/admin/smsconfig")
public class SmsConfigController {

    @Autowired
    private SMSConfig smsConfig;

    @Autowired
    private MailUtils utils;

    /**
     * 获取配置
     */
    @RequiresPermissions("sms_config")
    @RequestMapping("/edit")
    public String edit(Model model) {
        SmsConfig config = smsConfig.get_config(1L);
        model.addAttribute("model", config);
        return "admin/sms/edit";
    }

    /**
     * 更新配置
     */
    @RequiresPermissions("sms_config")
    @RequestMapping("/update")
    @ResponseBody
    public Map update(SmsConfig config) {
        HashMap<Object, Object> ret = new HashMap<>();
        smsConfig.update_config(config);
        final Thread thread = new Thread(() -> {
            try {
                utils.reGetSession();
                System.out.println("重新初始化邮件配置成功!");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        thread.setDaemon(true);
        thread.start();
        ret.put("msg", "更新成功!");
        ret.put("code", 0);
        return ret;
    }

}
